# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: my_marquee.py
  @time: 2021/6/7 10:46
  @desc:
"""
import sys
import PySide6
from PySide6.QtCore import Qt, QTimer, Slot
from PySide6.QtGui import QFont, QFontMetrics, QPainter, QColor
from PySide6.QtWidgets import QLabel


class MyMarquee(QLabel):
    def __init__(self, parent=None):
        super(MyMarquee, self).__init__(parent)
        self.text = "Hello World"
        self.m_curIndex = 0  # 当前角码
        self.new_font = QFont()
        self.new_font.setPointSize(self.new_font.pointSize() + 20)
        self.setFont(self.new_font)
        self.m_charWidth = QFontMetrics(self.new_font).maxWidth()
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_index)
        self.timer.start(200)
        self.resize(360, 145)

    def paintEvent(self, event: PySide6.QtGui.QPaintEvent) -> None:
        color = QColor()
        color.setHsv(0, 255, 255)
        painter = QPainter()
        painter.begin(self)
        painter.setPen(color)
        print("self.width()", self.width())
        print("self.m_charWidth", self.m_charWidth)
        print("self.m_curIndex", self.m_curIndex)
        painter.drawText(self.width() - self.m_charWidth * self.m_curIndex, 30, self.text)
        painter.end()

    def update_index(self):
        self.m_curIndex += 1
        if self.m_curIndex * self.m_charWidth > self.width():
            self.m_curIndex = 0
        self.update()
